package kitrobot.com.bed.Activity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import kitrobot.com.bed.Service.GetbedService;
import kitrobot.com.wechat_bottom_navigation.R;

public class ChangeActivity extends AppCompatActivity implements View.OnClickListener {
    private ListView lv_pc;
    private List<HashMap<String, Object>> bdata;
    String id=null;
    String token=null;
    private final String img="img";
    private  String bid="bid";
    private String bname="bname";
    String cbid=null;
    String cbname=null;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,  WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.change);
        SharedPreferences sp = getSharedPreferences("loginToken",
                Context.MODE_PRIVATE);
        id = sp.getString("id", "");
        String phone = sp.getString("phone", "");
        String  name = sp.getString("name", "");
        token = sp.getString("token", "");
        String type=sp.getString("type","");
        lv_pc= (ListView)findViewById(R.id.lv_pc);

        new Thread(){
            public void run(){
                final String result= GetbedService.GetbedByClientPost(id,token);
                if (result!=null){
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {

                            try {
                                //JSON字符串

                                //根据JSON字符串 获得JSON object对象
                                JSONObject jsonObject = new JSONObject(result);
                                //根据key 获取相应的value
                                String errno = jsonObject.getString("errno");
                                String errmsg="年龄:" + jsonObject.getString("errmsg");
                                JSONArray data = jsonObject.getJSONArray("data");


                                bdata = new ArrayList<HashMap<String, Object>>();
                                for (int i = 0; i < data.length(); i++) {
                                    HashMap<String, Object> mapItem = new HashMap<String, Object>();
                                    JSONObject jsonObject2 = (JSONObject) data.get(i);
                                    cbid = jsonObject2.getString("id");
                                    cbname = jsonObject2.getString("name");
                                    mapItem.put(bid, cbid);
                                    mapItem.put(bname, cbname);
                                    mapItem.put(img, R.drawable.icon01);
                                    bdata.add(mapItem);

                                }
                                SimpleAdapter simAdapter = new SimpleAdapter(
                                        ChangeActivity.this, bdata, R.layout.changelist,
                                        new String[] {  bname,img}, new int[] {
                                        R.id.tv_pcn,R.id.img });
                                lv_pc.setAdapter(simAdapter);


                            } catch (Exception e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                            }
                        }

                    });
                }
                else{
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            Toast.makeText(ChangeActivity.this,"结果"+result,Toast.LENGTH_LONG).show();
                        }
                    });
                }
            };

        }.start();
        lv_pc.setOnItemClickListener(new itemClick());
    }
    class itemClick implements OnItemClickListener{

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            // TODO Auto-generated method stub
            Map<String, String> infoMap = (Map<String, String>) parent.getItemAtPosition(position);
            System.out.println(infoMap.get("bid"));
            String bid=infoMap.get("bid");
            System.out.println(infoMap.get("bname"));
            Intent intent = new Intent(ChangeActivity.this, TransferActivity.class);
            intent.putExtra("bid",bid);
            startActivity(intent);
        }

    }

    public void Caidan1(View view){
        Intent intent=new Intent(this,MainActivity.class);
        intent.putExtra("flag",1);
        startActivity(intent);
    }
    public void Caidan2(View view){
        Intent intent=new Intent(this,MainActivity.class);
        intent.putExtra("flag",2);
        startActivity(intent);
    }
    public void Caidan3(View view){
        Intent intent=new Intent(this,MainActivity.class);
        intent.putExtra("flag",3);
        startActivity(intent);
    }
    public void Caidan5(View view){
        Intent intent=new Intent(this,MainActivity.class);
        intent.putExtra("flag",4);
        startActivity(intent);
    }
    public void Finish(View view){
        finish();
    }
    @Override
    public void onClick(View view) {

    }

}
